import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.2


//用于表头设置
Rectangle {
    id: listBk
    color: "white"

    property var colModel

    Component {
        //高亮条
        id: highlight
        Rectangle {
            color: "lightsteelblue"
            radius: 5
        }
    }

    Component {
        id: checkListDel2

        Item {
            id: wrapper
            height: 24
            width: 200

            Row {
                spacing: 4
                anchors.fill: wrapper
                CheckBox {
                    id: panelItem
                    anchors.verticalCenter: parent.verticalCenter
                    width: 40
                    text: index
                    checked: show === 1 ? true : false

                    onClicked: {
                        wrapper.ListView.view.currentIndex = index
                        show = (show === 1 ? 0 : 1)
                        colModel.setCheck(wrapper.ListView.view.currentIndex,
                                          panelItem.checked)
                        console.log(qsTr('ListView checkListDel currentIndex: "'
                                         + wrapper.ListView.view.currentIndex + '"')) //是否禁用后面的状态单选，
                    }
                }

                TextField {
                    id: editName
                    width: 120
                    text: name
                    onTextChanged: {
                        colModel.setName(index, editName.text)
                        //                        name = editName.text;
                    }
                }

                TextField {
                    id: editID
                    //                anchors.left: editName.left;
                    width: 120
                    text: enName
                    onTextChanged: {
                        colModel.setEnName(index, editID.text)
                        //                        enName = editID.text;
                    }
                }

                ExclusiveGroup {
                    id: gOrderState
                }
                RadioButton {
                    id: leftRD
                    anchors.verticalCenter: parent.verticalCenter
                    width: 40
                    text: qsTr("左")
                    exclusiveGroup: gOrderState
                    checked: align === 1 ? true : false

                    onClicked: {
                        colModel.setAlign(index, 1)
                    }
                }
                RadioButton {
                    id: midRD
                    anchors.verticalCenter: parent.verticalCenter
                    width: 40
                    text: qsTr("中")
                    checked: align === 2 ? true : false
                    exclusiveGroup: gOrderState
                    onClicked: {
                        colModel.setAlign(index, 2)
                    }
                }
                RadioButton {
                    id: rightRD
                    anchors.verticalCenter: parent.verticalCenter
                    width: 40
                    text: qsTr("右")
                    exclusiveGroup: gOrderState
                    checked: align === 3 ? true : false
                    onClicked: {
                        colModel.setAlign(index, 3)
                    }
                }

                //                ComboBox {
                //                    id:fmtSel;
                //                    model:    ListModel {
                //                        id:fmtModel
                //                        ListElement {text: " ";}
                //                        ListElement {text: "FmtCode";}
                //                        ListElement {text: "FmtDate";}
                //                        ListElement {text: "FmtTime";}
                //                        ListElement {text: "FmtMoney";}
                //                    }
                //                    width:80;
                //                    property bool bInit: false;
                ////                    currentIndex:find(fmt);
                //                    Component.onCompleted: {
                //                        fmtSel.currentIndex=fmtSel.find(fmt);
                ////                        console.log(qsTr(currentIndex.toString()+' TextField onCompleted fmtSel: "' + fmt + '"'));
                //                    }

                //                    onCurrentIndexChanged: {
                //                        if (currentText == "FmtCode"){
                //                            editFmtPara.readOnly = false;
                //                        }
                //                        else {
                //                            editFmtPara.readOnly = true;
                //                            if (bInit)
                //                                editFmtPara.text = "";
                //                        }

                //                        if (bInit){
                //                            colModel.setFmt(index, currentText);
                ////                            console.log(qsTr(index.toString()+' TextField onCurrentIndexChanged fmtSel: "' + currentText + '"'));
                //                        }
                //                        bInit= true;
                //                    }
                //                }

                //                TextField {
                //                    id:editFmtPara;
                //                    width:150;
                //                    anchors.topMargin: 2;
                //                    anchors.bottomMargin: 2;
                //                    text:fmtExtra;
                //                    style:TextFieldStyle {
                //                        background:Rectangle {
                //                            color: control.readOnly? "lightgray": "white" ;
                //                            border.color: "#333";
                //                            border.width: 1;
                //                        }
                //                    }
                //                    onTextChanged: {
                ////                        fmtExtra = editFmtPara.text;
                //                        colModel.setFmtExtra(index, editFmtPara.text);
                ////                        console.log(qsTr('ListView checkListDel editFmtPara: "' + editFmtPara.text + '"'));
                //                    }

                //                }
            }
        }
    }

    ScrollView {
        anchors.fill: parent

        ListView {
            id: columnList
            //            width: 220;
            anchors.fill: parent
            //                        anchors.left:bottomArea.left;
            anchors.leftMargin: 5
            //                        anchors.top:bottomArea.top;
            anchors.topMargin: 3
            anchors.bottomMargin: 3
            anchors.rightMargin: 5
            //                        anchors.bottom: bottomArea.bottom;
            highlight: highlight //关联高亮条
            clip: true
            focus: true //可以获得焦点，这样就可以响应键盘了
            spacing: 4

            model: colModel
            //                delegate: petDelegate;
            delegate: checkListDel2
        }
    }

    function moveUp() {
        if (0 != columnList.currentIndex) {
            colModel.moveUp(columnList.currentIndex)
            columnList.currentIndex--
        }
    }

    function moveDown() {
        if (columnList.currentIndex != columnList.count - 1) {
            colModel.moveDown(columnList.currentIndex)
            columnList.currentIndex++
        }
    }

    function insertRow() {
        colModel.insertRow()
    }

    function removeCheckItem() {
        colModel.deleteData(columnList.currentIndex)
    }
}
